const path = require('path')
const framework = require('../../framework')
const _ = require('lodash')
const jsonfile = require('jsonfile')

module.exports = function configPlugin(config) {
  const plugin = framework.plugin.wrap(this, config.serviceName)

  plugin.add('role:config,cmd:read', function (msg, respond) {
    if (typeof msg.fileName !== 'string') {
      return respond(new Error('缺少fileName参数！'))
    }
    const appConfigPath = framework.common.getAppConfigPath(
      path.join('config', path.sep, msg.fileName)
    )
    const appConfig = appConfigPath
      ? jsonfile.readFileSync(appConfigPath)
      : appConfigPath

    return respond({
      config: appConfig,
      services: config.services
    })
  })
}
